//
//  DetailViewController.m
//  SweetComics
//
//  Created by dlios on 15-1-31.
//  Copyright (c) 2015年 LeeZhe. All rights reserved.
//

#import "DetailViewController.h"
#import "ComicViewController.h"
#import "UIImageView+WebCache.h"
#import "DataBaseHandler.h"

@interface DetailViewController ()

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, assign) NSInteger i;
@property (nonatomic, assign) NSInteger flag;

@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = [NSString stringWithFormat: @"%@",self.comic.title];
    [self.dbHandler glanceComic:self.comic number:self.i count:0];
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(0, 1000);
    [_scrollView release];
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 130)];
//    _imageView.backgroundColor = [UIColor cyanColor];
    _imageView.image = [UIImage imageNamed:@"kuu.png"];
    // 利用 SDwebImage 请求图片
    NSString *str = self.comic.cover_url;
    NSURL *url = [NSURL URLWithString:str];
    [_imageView setImageWithURL:url];
    [self.scrollView addSubview:_imageView];
    [_imageView release];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 25, self.view.frame.size.width - 160, 30)];
    //    _nameLabel.backgroundColor = [UIColor redColor];
    _nameLabel.text = self.comic.title;
    _nameLabel.font = [UIFont systemFontOfSize:20];
    _nameLabel.font = [UIFont boldSystemFontOfSize:20];
    [self.scrollView addSubview:self.nameLabel];
    [_nameLabel release];
    // 作者
    self.authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 65, self.view.frame.size.width - 160, 20)];
    //    _authorLabel.backgroundColor = [UIColor redColor];
    _authorLabel.text = [NSString stringWithFormat:@"作者：%@", self.comic.artist_name];
    _authorLabel.font = [UIFont systemFontOfSize:15];
    [self.scrollView addSubview:self.authorLabel];
    [_authorLabel release];
    // 类型
    self.typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 95, self.view.frame.size.width - 160, 20)];
    //    _typeLabel.backgroundColor = [UIColor redColor];
    _typeLabel.text = [NSString stringWithFormat: @"类型：%@", self.comic.type];
    _typeLabel.font = [UIFont systemFontOfSize:15];
    [self.scrollView addSubview:self.typeLabel];
    [_typeLabel release];
    // 评分
    self.scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 125, self.view.frame.size.width - 160, 20)];
    //    _scoreLabel.backgroundColor = [UIColor redColor];
    _scoreLabel.text = [NSString stringWithFormat: @"评分：%@", self.comic.grade_ave];
    _scoreLabel.font = [UIFont systemFontOfSize:15];
    [self.scrollView addSubview:self.scoreLabel];
    [_scoreLabel release];
    // 收藏按钮
//    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    NSMutableArray *arrc = [self.dbHandler selectCollect];
    for (ComicList *comic in arrc) {
        if (self.comic == comic) {
            self.flag = 1;
        }
        else
            _flag = 0;
    }
    if (self.flag) {
        [button1 setTitle:@"已收藏" forState:UIControlStateNormal];
        button1.backgroundColor = [UIColor redColor];
        button1.layer.cornerRadius = 5;
        button1.frame = CGRectMake(20, 170, (self.view.frame.size.width - 120) / 2, 30);
        button1.tintColor = [UIColor whiteColor];
        [button1 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button1];
        
    }else{
        [button1 setTitle:@"收藏" forState:UIControlStateNormal];
        button1.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:0.7];
        button1.layer.cornerRadius = 5;
        button1.frame = CGRectMake(20, 170, (self.view.frame.size.width - 120) / 2, 30);
        button1.tintColor = [UIColor whiteColor];
        [button1 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button1];
    }
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setTitle:@"开始阅读" forState:UIControlStateNormal];
    button2.layer.cornerRadius = 5;
    button2.frame = CGRectMake(20 + (self.view.frame.size.width - 120) / 2 + 80, 170, (self.view.frame.size.width - 120) / 2, 30);
    button2.tintColor = [UIColor whiteColor];
    button2.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:0.7];
    [button2 addTarget:self action:@selector(buttonAction2:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:button2];
    // 详情
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 100, 25)];
    label1.text = @"详情";
    [self.scrollView addSubview:label1];
    label1.font = [UIFont boldSystemFontOfSize:18];
    [label1 release];
    
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 265, self.view.frame.size.width - 40, 150)];
    //    _contentLabel.backgroundColor = [UIColor redColor];
    _contentLabel.text = [NSString stringWithFormat:@"%@", self.comic.brief_intrd];
    _contentLabel.font = [UIFont systemFontOfSize:15];
    _contentLabel.numberOfLines = 0;
    [_contentLabel sizeToFit];
    [self.scrollView addSubview:self.contentLabel];
    [_contentLabel release];
    // 目录
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, self.contentLabel.frame.size.height + 285, 100, 25)];
    label2.text = @"目录";
    label2.font = [UIFont boldSystemFontOfSize:18];
    [self.scrollView addSubview:label2];
    [label2 release];
    self.scrollView.contentSize = CGSizeMake(0, self.contentLabel.frame.size.height + 350 + 30 * ([self.comic.lated_seqno integerValue] / 4));
    self.scrollView.bounces = NO;
    for (self.i = 1; _i <= [self.comic.lated_seqno integerValue]; _i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(20 + ((self.view.frame.size.width - 20) / 4) * ((_i - 1)% 4), self.contentLabel.frame.size.height + 320 + 30 * ((_i - 1) / 4), (self.view.frame.size.width - 80) / 4, 20);
        button.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:0.7];
        button.tintColor = [UIColor whiteColor];
        button.layer.cornerRadius = 5;
        [button setTitle:[NSString stringWithFormat:@"第%ld话", (long)_i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(comicButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrollView addSubview:button];
        
    }
    self.dbHandler = [DataBaseHandler shareInstance];
    
}
// 收藏
- (void)buttonAction1:(UIButton *)button
{
    if ([self.dbHandler insertComic:self.comic]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        button.backgroundColor = [UIColor redColor];
        [button setTitle:@"已收藏" forState:UIControlStateNormal];
        [alert show];
        [alert release];
    }else {
        button.backgroundColor = [UIColor redColor];
        [button setTitle:@"已收藏" forState:UIControlStateNormal];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"收藏失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}

- (void)buttonAction2:(UIButton *)button
{
    self.i = 1;
    self.hidesBottomBarWhenPushed = YES;
    ComicViewController *comicVC = [[ComicViewController alloc] init];
    comicVC.comic = self.comic;
    comicVC.i = self.i;
    [self.dbHandler glanceComic:self.comic number:self.i count:1];
    [self.navigationController pushViewController:comicVC animated:NO];
    self.hidesBottomBarWhenPushed = NO;
    [comicVC release];
}

- (void)comicButton:(UIButton *)button
{
    NSScanner *scanner = [NSScanner scannerWithString:button.titleLabel.text];
    [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
    NSInteger number = 0;
    [scanner scanInteger:&number];
//    NSLog(@"number : %ld", (long)number);
    self.i = number;
    self.hidesBottomBarWhenPushed = YES;
    ComicViewController *comicVC = [[ComicViewController alloc] init];
    comicVC.comic = self.comic;
    comicVC.i = self.i;
    [self.dbHandler glanceComic:self.comic number:self.i count:1];
    
    [self.navigationController pushViewController:comicVC animated:NO];
    self.hidesBottomBarWhenPushed = NO;
    
    [comicVC release];
}

- (void)dealloc
{
    [_comic release];
    [_imageView release];
    [_nameLabel release];
    [_authorLabel release];
    [_typeLabel release];
    [_scoreLabel release];
    [_contentLabel release];
    [super dealloc];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
